package com.platform.cdcs.fragment.detail;

import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.platform.cdcs.R;
import com.platform.cdcs.fragment.account.AccountListFragment;
import com.platform.cdcs.model.BaseObjResponse;
import com.platform.cdcs.model.MockObj;
import com.platform.cdcs.model.RefershEvent;
import com.platform.cdcs.model.ReportInfo;
import com.platform.cdcs.tool.Constant;
import com.platform.cdcs.tool.FragmentUtil;
import com.platform.cdcs.tool.ViewTool;
import com.trueway.app.uilib.fragment.BaseFragment;
import com.trueway.app.uilib.tool.Utils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;
import okhttp3.Call;

/**
 * Created by holytang on 2017/11/20.
 */
public class ReportDetailFragment extends BaseFragment {

    private int type;
    private View rootView;
    private String fileReportId;
    private TextView btnView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        type = getArguments().getInt("type");
        fileReportId = getArguments().getString("id");
    }

    @Override
    public void initView(View view) {
        initLoadImg(view.findViewById(R.id.load));
        if (type == 2) {
            setTitle("出入库上报详情");
        } else if (type == 3) {
            setTitle("发票上报详情");
        } else if (type == 4) {
            setTitle("代报申请详情");
        } else {
            setTitle("详情");
        }
        getToolBar().setNavigationIcon(R.mipmap.icon_back);
        getToolBar().setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getActivity().finish();
            }
        });
        rootView = view;
        rootView.findViewById(R.id.text).setVisibility(View.GONE);

        requestDetail();
    }

    @Override
    public int layoutId() {
        return R.layout.five_layout;
    }

    private void setData(ReportInfo info) {
        rootView.findViewById(R.id.button3).setVisibility(View.GONE);
        rootView.findViewById(R.id.button4).setVisibility(View.GONE);
        rootView.findViewById(R.id.button5).setVisibility(View.GONE);
        btnView = (TextView) rootView.findViewById(R.id.text);
        btnView.setVisibility(View.VISIBLE);
        if (info.getHandled() == 0) {
            btnView.setText("标记为已处理");
        } else {
            btnView.setText("已处理");
            btnView.setEnabled(false);
        }
        btnView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                hadClick();
            }
        });
        LayoutInflater inflater = LayoutInflater.from(getContext());
        LinearLayout root1 = (LinearLayout) rootView.findViewById(R.id.button1);
        ViewTool.setFourItemOnly(inflater, root1, new String[]{"文件名称", info.getFileName()});
        ViewTool.setFourItem(inflater, root1, new String[]{"文件编号", "上报人"}, new String[]{info.getId(), info.getFillMan()});
        ViewTool.setFourItem(inflater, root1, new String[]{"上报来源", "上报时间"}, new String[]{info.getReportSource(), info.getReportDate()});

        View fatherView = ViewTool.setFourItem(inflater, root1, new String[]{"处理时长", "处理结果"}, new String[]{info.getHandleTime(), info.getStatusText()});
        TextView stateView = (TextView) fatherView.findViewById(R.id.text2);
        if ("99".equals(info.getStatus())) {
            stateView.setTextColor(ContextCompat.getColor(getContext(),R.color.text_red));
            stateView.setTextSize(16);
        }
        LinearLayout root2 = (LinearLayout) rootView.findViewById(R.id.button2);
        View successView = inflater.inflate(R.layout.left_right, null);
        TextView successLV = (TextView) successView.findViewById(R.id.title);
        successLV.setText("处理成功");
        successLV.setCompoundDrawablePadding(Utils.convertDIP2PX(getContext(), 10));
        successLV.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.icon_state_success, 0, 0, 0);

        TextView successRV = (TextView) successView.findViewById(R.id.text);
        successRV.setText(String.valueOf(info.getSuccessNum()));
        root2.addView(successView);

        View lineView = new View(getContext());
        LinearLayout.LayoutParams lineLP = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1);
        lineView.setLayoutParams(lineLP);
        lineView.setBackgroundColor(ContextCompat.getColor(getContext(),R.color.gray_text));
        root2.addView(lineView);

        View failView = inflater.inflate(R.layout.left_right, null);
        TextView failLV = (TextView) failView.findViewById(R.id.title);
        failLV.setText("处理失败");
        failLV.setCompoundDrawablePadding(Utils.convertDIP2PX(getContext(), 10));
        failLV.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.icon_state_fail, 0, 0, 0);

        TextView failRV = (TextView) failView.findViewById(R.id.text);
        failRV.setText(String.valueOf(info.getFailNum()));
        if (info.getFailNum() > 0) {
            failRV.setCompoundDrawablePadding(Utils.convertDIP2PX(getContext(), 10));
            failRV.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.icon_right, 0);
            failView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (type == 2) {
                        FragmentUtil.navigateToInNewActivity(getActivity(), InOutErrFragment.class, getArguments());
                    } else {
                        FragmentUtil.navigateToInNewActivity(getActivity(), TicketErrFragment.class, getArguments());
                    }
                }
            });
        }
        root2.addView(failView);
    }

//    private void setTicketData() {
//        rootView.findViewById(R.id.button3).setVisibility(View.GONE);
//        rootView.findViewById(R.id.button4).setVisibility(View.GONE);
//        rootView.findViewById(R.id.button5).setVisibility(View.GONE);
//        TextView btnView = (TextView) rootView.findViewById(R.id.text);
//        btnView.setText("标记为已处理");
//        LayoutInflater inflater = LayoutInflater.from(getContext());
//        LinearLayout root1 = (LinearLayout) rootView.findViewById(R.id.button1);
//        ViewTool.setFourItemOnly(inflater, root1, new String[]{"文件名称", ""});
//        ViewTool.setFourItem(inflater, root1, new String[]{"文件编号", "上报人"}, new String[]{"", ""});
//        ViewTool.setFourItem(inflater, root1, new String[]{"上报来源", "上报时间"}, new String[]{"", ""});
//
//        View fatherView = ViewTool.setFourItem(inflater, root1, new String[]{"处理时长", "处理结果"}, new String[]{"", ""});
//        TextView stateView = (TextView) fatherView.findViewById(R.id.text2);
//        stateView.setTextSize(16);
//        stateView.setTextColor(getResources().getColor(R.color.text_red));
//
//        LinearLayout root2 = (LinearLayout) rootView.findViewById(R.id.button2);
//        View successView = inflater.inflate(R.layout.left_right, null);
//        TextView successLV = (TextView) successView.findViewById(R.id.title);
//        successLV.setText("处理成功");
//        successLV.setText("100");
//        successLV.setCompoundDrawablePadding(Utils.convertDIP2PX(getContext(), 10));
//        TextView successRV = (TextView) successView.findViewById(R.id.text);
//        successRV.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.icon_state_success, 0, 0, 0);
//        root2.addView(successView);
//
//        View lineView = new View(getContext());
//        LinearLayout.LayoutParams lineLP = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 1);
//        lineView.setLayoutParams(lineLP);
//        lineView.setBackgroundColor(getResources().getColor(R.color.gray_text));
//        root2.addView(lineView);
//
//        View failView = inflater.inflate(R.layout.left_right, null);
//        TextView failLV = (TextView) successView.findViewById(R.id.title);
//        failLV.setText("处理失败");
//        failLV.setCompoundDrawablePadding(Utils.convertDIP2PX(getContext(), 10));
//        failLV.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.icon_state_fail, 0, 0, 0);
//        failLV.setText("20");
//
//        TextView failRV = (TextView) successView.findViewById(R.id.text);
//        failRV.setText("1");
//        failRV.setCompoundDrawablePadding(Utils.convertDIP2PX(getContext(), 10));
//        failRV.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.mipmap.icon_right, 0);
//        root2.addView(failView);
//    }

    private void setNotifyData() {
        rootView.findViewById(R.id.button5).setVisibility(View.GONE);
        rootView.findViewById(R.id.text).setVisibility(View.GONE);
        LayoutInflater inflater = LayoutInflater.from(getContext());
        LinearLayout root1 = (LinearLayout) rootView.findViewById(R.id.button1);
        ViewTool.setFourItemOnly(inflater, root1, new String[]{"代报经销商代码", "34"});
        ViewTool.setFourItemOnly(inflater, root1, new String[]{"代报经销商名称", "34"});
        ViewTool.setFourItemOnly(inflater, root1, new String[]{"是否合并库存", "34"});

        LinearLayout root2 = (LinearLayout) rootView.findViewById(R.id.button2);
        ViewTool.setFourItem(inflater, root2, new String[]{"状态", "处理结果"}, new String[]{"申请中", "--"});
        ViewTool.setFourItemOnly(inflater, root1, new String[]{"备注", "34"});

        LinearLayout root3 = (LinearLayout) rootView.findViewById(R.id.button3);
        ViewTool.setFourItem(inflater, root3, new String[]{"申请时间", "审核时间"}, new String[]{"申请中", "--"});

    }

    private void requestDetail() {
        showLoadImg();
        if (type == 2 || type == 3) {
            Map<String, String> param = new HashMap<>();
            param.put("fileReportId", fileReportId);
            getHttpClient().post().url(Constant.FILE_REPORT_INFO).params(Constant.makeParam(param)).build().execute(new StringCallback() {
                @Override
                public void onError(Call call, Exception e, int i) {
                    dismissLoadImg();
                    Utils.showToast(getContext(), R.string.server_error);
                }

                @Override
                public void onResponse(String s, int i) {
                    try {
                        dismissLoadImg();
                        if (getContext() == null) {
                            return;
                        }
                        Type type = new TypeToken<BaseObjResponse<FileReportInfo>>() {
                        }.getType();
                        BaseObjResponse<FileReportInfo> response = new Gson().fromJson(s, type);
                        if ("1".equals(response.getResult().getCode())) {
                            setData(response.getResult().getFileReportInfo());
                        } else {
                            Utils.showToast(getContext(), response.getResult().getMsg());
                        }
                    } catch (Exception e) {
                        Utils.showToast(getContext(), "解析数据失败");
                    }
                }
            });
        }
    }

    private void hadClick() {
        showLoadImg();
        Map<String, String> param = new HashMap<>();
        param.put("id", fileReportId);
        getHttpClient().post().url(Constant.MARK_HANDLED).params(Constant.makeParam(param)).build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int i) {
                dismissLoadImg();
                Utils.showToast(getContext(), R.string.server_error);
            }

            @Override
            public void onResponse(String s, int i) {
                try {
                    dismissLoadImg();
                    if (getContext() == null) {
                        return;
                    }
                    Type type = new TypeToken<BaseObjResponse<MockObj>>() {
                    }.getType();
                    BaseObjResponse<MockObj> response = new Gson().fromJson(s, type);
                    if ("1".equals(response.getResult().getCode())) {
                        Utils.showToast(getContext(), "已处理");
                        btnView.setText("已处理");
                        btnView.setEnabled(false);
                    } else {
                        Utils.showToast(getContext(), response.getResult().getMsg());
                    }
                } catch (Exception e) {
                    Utils.showToast(getContext(), "解析数据失败");
                }
            }
        });
    }

    private class FileReportInfo extends MockObj {
        private ReportInfo fileReportInfo;

        public ReportInfo getFileReportInfo() {
            return fileReportInfo;
        }

        public void setFileReportInfo(ReportInfo fileReportInfo) {
            this.fileReportInfo = fileReportInfo;
        }
    }
}
